<script setup>
import { ref, onMounted } from 'vue'
import { getAllCategoriesAPI, getAllTagsAPI } from '@/api'
import errorLogger from '@/utils/errorLogger'

// 直接声明两个模型，分别对应 v-model:category 和 v-model:tags
const category = defineModel('category', { type: String })
const tags = defineModel('tags', { type: Array })

const categoryOptions = ref([])
const tagOptions = ref([])

const fetchData = async () => {
  try {
    const [cat, tag] = await Promise.all([
      getAllCategoriesAPI(),
      getAllTagsAPI(),
    ])
    categoryOptions.value = cat.data
    tagOptions.value = tag.data
  } catch (err) {
    errorLogger(err)
  }
}

onMounted(fetchData)
</script>

<template>
  <el-row :gutter="20">
    <el-col :span="12">
      <el-form-item label="文章分类">
        <el-select
          v-model="category"
          placeholder="请选择文章分类"
          style="width: 100%">
          <el-option
            v-for="item in categoryOptions"
            :key="item._id"
            :label="item.name"
            :value="item._id" />
        </el-select>
      </el-form-item>
    </el-col>

    <el-col :span="12">
      <el-form-item label="文章标签">
        <el-select
          v-model="tags"
          multiple
          placeholder="请选择文章标签"
          style="width: 100%">
          <el-option
            v-for="item in tagOptions"
            :key="item._id"
            :label="item.name"
            :value="item._id" />
        </el-select>
      </el-form-item>
    </el-col>
  </el-row>
</template>
